#!/usr/bin/env python3

import sys

def fix_config_file(file_path):
    """修复配置文件格式问题"""
    try:
        # 读取文件内容
        with open(file_path, 'r', encoding='utf-8') as f:
            lines = f.readlines()
        
        # 修复第一行，确保有左方括号
        if lines and lines[0].strip() == 'CabrilloConfig]':
            lines[0] = '[CabrilloConfig]\n'
            print("已修复配置文件第一行")
        
        # 写回文件
        with open(file_path, 'w', encoding='utf-8') as f:
            f.writelines(lines)
        
        print(f"配置文件 {file_path} 已修复")
        return True
    except Exception as e:
        print(f"修复配置文件时出错: {e}")
        return False

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print("用法: python3 fix_config.py <配置文件路径>")
        sys.exit(1)
    
    file_path = sys.argv[1]
    if fix_config_file(file_path):
        print("配置文件修复成功")
    else:
        print("配置文件修复失败")
        sys.exit(1)